package com.ncmmall.application.business.withdraw;

import com.feijin.commons.lang.ArithUtils;
import com.ncmmall.domain.business.setting.SystemSetting;
import com.ncmmall.domain.business.trading.MerchantWithdrawApply;
import com.ncmmall.domain.business.trading.MerchantWithdrawApplyRepository;
import com.ncmmall.domain.business.trading.QMerchantWithdrawApply;
import com.ncmmall.domain.business.trading.QTradingLog;
import com.ncmmall.domain.business.trading.TradingLog;
import com.ncmmall.domain.business.trading.TradingLogRepository;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.domain.security.user.WebUserRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

/**
 * @author linxs
 * @Description: 商户提现业务
 * @Date: 2019-09-19 15:32
 * 结算流程说明：商户发起申请、财务审核通过、商户提交发票信息、财务确认并线下结算，确认结算单完成
 */
@Service
@Transactional
public class MerchantWithdrawApplication {

    @Autowired
    private MerchantWithdrawApplyRepository merchantWithdrawApplyRepository;

    @Autowired
    private TradingLogRepository tradingLogRepository;

    @Autowired
    private WebUserRepository merchantRepository;

    @Autowired
    private QueryChannelService queryChannelService;


    /**
     * 申请提现
     */
    public void applyWithdraw(MerchantWithdrawApply apply) {

        SystemSetting setting = queryChannelService.queryOneEntity("select * from mall_system_setting limit 1", Collections.emptyMap(), SystemSetting.class);

        apply.setApplyTime(new Timestamp(System.currentTimeMillis()));
        apply.setStatus(1);
        merchantWithdrawApplyRepository.save(apply);

        TradingLog tradingLog = new TradingLog();
        tradingLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
        tradingLog.setMerchant(apply.getMerchant());
        tradingLog.setType(3);

        if (apply.getMerchant().getRole().getName().equals("MERCHANT")) {
            tradingLog.setAmount(apply.getAmount());
            tradingLog.setNote("手续费" + ArithUtils.mul(apply.getAmount(), setting.getInvoiceScale()) + ", 提现金额：" + apply.getAmount());
        }

        if (apply.getMerchant().getRole().getName().equals("SUPPLIER")) {
            tradingLog.setAmount(apply.getAmount());
            tradingLog.setNote("手续费" + ArithUtils.mul(apply.getAmount(), setting.getSupplierInvoiceScale()) + ", 提现金额：" + apply.getAmount());
        }

        tradingLogRepository.save(tradingLog);

        WebUser merchant = queryChannelService.findOne(QWebUser.webUser.id.eq(apply.getMerchant().getId()), WebUser.class);
        merchant.setBalance(ArithUtils.sub(merchant.getBalance(), apply.getAmount()));
        merchantRepository.save(merchant);

    }


    /**
     * 申请提现审核通过
     */
    public void applyPass(MerchantWithdrawApply params) {
        MerchantWithdrawApply apply = queryChannelService.findOne(QMerchantWithdrawApply.merchantWithdrawApply.id.eq(params.getId()), MerchantWithdrawApply.class);
        apply.setStatus(2);
        merchantWithdrawApplyRepository.save(apply);
    }


    /**
     * 申请提现审核拒绝
     *
     * @param params
     */
    public void applyReject(MerchantWithdrawApply params) {
        MerchantWithdrawApply apply = queryChannelService.findOne(QMerchantWithdrawApply.merchantWithdrawApply.id.eq(params.getId()), MerchantWithdrawApply.class);
        apply.setNote(params.getNote());
        apply.setStatus(3);
        merchantWithdrawApplyRepository.save(apply);

        TradingLog tradingLog = new TradingLog();
        tradingLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
        tradingLog.setMerchant(apply.getMerchant());
        tradingLog.setType(4);
        tradingLog.setAmount(apply.getAmount());
        tradingLogRepository.save(tradingLog);

        WebUser merchant = queryChannelService.findOne(QWebUser.webUser.id.eq(apply.getMerchant().getId()), WebUser.class);
        merchant.setBalance(ArithUtils.add(merchant.getBalance(), apply.getAmount()));
        merchantRepository.save(merchant);

    }

    /**
     * 提交发票信息
     *
     * @param params
     */
    public void invoiceInfo(MerchantWithdrawApply params) {
        MerchantWithdrawApply apply = queryChannelService.findOne(QMerchantWithdrawApply.merchantWithdrawApply.id.eq(params.getId()), MerchantWithdrawApply.class);
        apply.setInvoice(params.getInvoice());
        apply.setStatus(4);
        apply.setExpressage(params.getExpressage());
        apply.setExpressageNo(params.getExpressageNo());
        apply.setInvoiceNo(params.getInvoiceNo());
        apply.setExpressageType(params.getExpressageType());

        merchantWithdrawApplyRepository.save(apply);
    }

    /**
     * 结算完成
     *
     * @param params
     */
    public void finish(MerchantWithdrawApply params) {
        MerchantWithdrawApply apply = queryChannelService.findOne(QMerchantWithdrawApply.merchantWithdrawApply.id.eq(params.getId()), MerchantWithdrawApply.class);
        apply.setStatus(5);
        apply.setFinalTime(new Timestamp(System.currentTimeMillis()));
        merchantWithdrawApplyRepository.save(apply);


        List<TradingLog> tradings = queryChannelService.findAll(QTradingLog.tradingLog.withdraw.eq(apply), TradingLog.class);
        for (TradingLog trading : tradings) {
            trading.setStatus(3);
            tradingLogRepository.save(trading);
        }

    }

}
